﻿// <copyright file="HomeController.cs">
//  Copyright (c) 2008 Eric Patey
//
//  Permission is hereby granted, free of charge, to any person
//  obtaining a copy of this software and associated documentation
//  files (the "Software"), to deal in the Software without
//  restriction, including without limitation the rights to use,
//  copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the
//  Software is furnished to do so, subject to the following
//  conditions:
//
//  The above copyright notice and this permission notice shall be
//  included in all copies or substantial portions of the Software.
//
//  THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
//  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
//  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
//  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
//  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
//  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
//  OTHER DEALINGS IN THE SOFTWARE.
// </copyright>

namespace WaringAuction.Controllers
{
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Web.Mvc;
    using EricUtilities.Azure;
    using WaringAuction.Models;
    using WaringAuction.Views.Home;

    [HandleError]
    public class HomeController : ControllerBase
    {
        public ActionResult Index()
        {
            ViewData["Title"] = "Home Page";

            return View();
        }

        public ActionResult Details(int itemNumber)
        {
            this.ViewData["Title"] = "Items Details";
            this.ViewData["blobContainerUri"] = this.BlobContainer.ContainerUri.ToString() + "/";

            AuctionDataServiceContext ctx = new AuctionDataServiceContext(this.TableAccountInfo);
            AuctionItem item = ctx.AuctionItems.Where<AuctionItem>(i => i.PartitionKey == itemNumber.ToString()).SingleOrDefault<AuctionItem>();
            if (item == null)
            {
                this.Response.StatusCode = (int)HttpStatusCode.NotFound;
                return null;
            }
            else
            {
                return View(item);
            }
        }

        /// <summary>
        /// Render a view to display auction items.
        /// </summary>
        /// <param name="ct">Pagination continuation token. Omit for the first page.</param>
        /// <returns>The ActionResult to be rendered.</returns>
        public ActionResult Items(string ct, bool? showAll)
        {
            this.ViewData["blobContainerUri"] = this.BlobContainer.ContainerUri.ToString() + "/";
            AuctionDataServiceContext ctx = new AuctionDataServiceContext(this.TableAccountInfo);

            string nextContinuationToken;
            IQueryable<AuctionItem> itemsQuery = ctx.AuctionItems;
            bool _showAll = showAll ?? false;
            if (!_showAll)
            {
                itemsQuery = itemsQuery.Where(i => i.Visible == true);
            }
            IEnumerable<AuctionItem> pagedItems = itemsQuery.ExecutePaged(4, ct, out nextContinuationToken);
            if (nextContinuationToken != null)
            {
                ViewData["continuation"] = nextContinuationToken;
                ViewData["showAll"] = _showAll;
            }

            ViewData["Title"] = "Auction Items";
            return View(new ItemsViewData()
            {
                Items = pagedItems
            });
        }
    }
}
